using Godot;
using GoGameClient.script.global;
using GoGameClient.script.resources;

namespace GoGameClient.script.node.create;

public partial class RoleSelectLayout : HBoxContainer
{
    private TextureRect _roleBackground;
    private TextureRect _introduceBackground;
    private string[] _roleName = new string[6];



    [Signal]
    public delegate void RoleSelectEventHandler(string roleName);


    public override void _Ready()
    {
        _roleBackground = GetParent().GetNode<TextureRect>("RoleBackground");
        _introduceBackground = GetParent().GetNode<TextureRect>("IntroduceBackground");
        SetRace(RoleResources.Race.Ren);
    }

    private void OnClick(int index)
    {
        SetRoleBackground(index - 1);
        // 获取所有子节点
        var children = GetChildren();

        // 为每个子节点添加鼠标点击事件处理器
        foreach (var node in children)
        {
            var child = (RoleButton)node;
            child.SetVisible(false);
        }
    }

    private void SetRoleBackground(int index)
    {
        EmitSignal(SignalName.RoleSelect, _roleName[index]);
        _roleBackground.Texture =
            (Texture2D)GD.Load($"res://resources/role/{_roleName[index]}/图片/人物背景.png");
        _introduceBackground.Texture =
            (Texture2D)GD.Load($"res://resources/role/{_roleName[index]}/图片/人物介绍.png");
    }


    public void SetRace(RoleResources.Race race)
    {
        var roles = ResourcesControl.Instance().GetRoleResources().GetRole(race);
        var children = GetChildren();
        for (var i = 0; i < roles.Count; i++)
        {
            _roleName[i] = roles[i].Name;
            var marginContainer = (TextureButton)children[i];
            marginContainer.TextureNormal =
                GD.Load<Texture2D>($"res://resources/role/{roles[i].Name}/图片/创建头像.png");
            marginContainer.GetNode<AnimatedSprite2D>("AnimatedSprite2D").Visible = i == 0;
        }

        SetRoleBackground(0);
    }
}